home *** CD-ROM | disk | FTP | other *** search
/ Creative Computers / Creative Computers CD-ROM, Volume 1 (Legendary Design Technologies, Inc.)(1994).iso / text / misc / ctutor1.txt.pp / ctutor1.txt
INI File  |  1994-11-17  |  9KB  |  180 lines

  1. [Any key to pause, ^C to abort]
  2.  
  3.  
  4.  
  5. Welcome the first installment of The Hack C Tutorial.
  6.  
  7. The format of these tutorials will based on the Q/A format, with an imaginary
  8. "student"'s question and an answer.
  9.  
  10. We assume that you more or less know BASIC, and have a basic idea of how it
  11. works.
  12.  
  13. So, let's begin.
  14.  
  15.  
  16.  
  17. Q:  So.  I've heard a lot about C, and it is supposed to be a very popular
  18.     language.  Is this true?
  19. A:  Well, yes.  Many professionals and amateurs use it, mostly because of its
  20.     speed and efficiency, which surpasses many others.  It was brought over
  21.     from the UNIX world, and there it gained a large base of followers.
  22.  
  23. Q:  How come a language can be faster than another?
  24. A:  The answer is relatively complex..  To begin with, we have to differentiate
  25.     between "interpreted" and "compiled" languages.  BASIC is usually
  26.     interpreted while C usually compiled.  The main difference is the time when
  27.     the program is translated into machine language, the only language that can
  28.     be run by the microprocessor.  Interpreted languages translate the lines of
  29.     code individually, every time they are executed.  The translation takes a
  30.     lot of time, because it is a very complicated operation.  So, in BASIC, for
  31.     instance, if you say:
  32.         FOR I=1 to 2000
  33.           PRINT I,SQR(I)*I/2
  34.         NEXT I
  35.     The line "PRINT..." will be translated to the same machine language
  36.     instructions 2000 times.  This wastes a lot of time.  Compiled languages,
  37.     however, only translate each line ONCE, and store the machine language code.
  38.     Once the compiler translates the whole program, it is saved on disk usually,
  39.     and can be run with no further intervention from the compiler.  Because each
  40.     program line was already translated, even if a line is executed thousands of
  41.     times, there will be NO time wasted translating it again and again.
  42.  
  43. Q:  Is this the only difference?
  44. A:  Well, not really.  Another place where interpreted languages are slowed down
  45.     is when they are using variables.  In the preceding example, for instance,
  46.     the variable "I" is looked for over 10 000 times during the duration of the
  47.     loop.  10 000!  And since BASIC interpreters usually have to look through
  48.     internal lists to find variables, it slows them down a lot.  Compiled
  49.     languages, however, only have to look for a variable ONCE, and whenever that
  50.     variable is referenced sometime later, the compiler will substitute the
  51.     previously found location in memory of that variable, and will never have to
  52.     explicitly search for it again and again.  This is the other major speed
  53.     improvement.
  54.  
  55. Q:  Hmm..  Tell me about variables.  Are they the same in C as they are in
  56.     BASIC?
  57. A:  Well, almost, except in C one has a much larger selection of variable types
  58.     than in BASIC..  Here is a list of the variable types allowed in BASIC:
  59.         Integer             - -32768 to +32767          - eg. 391
  60.         Floating point      - -far to +far              - eg. -293.372
  61.         String              - characters                - eg. "Hello There"
  62.         Array               - A group of any variable type
  63.     Arrays are a special type, as you know, in that they can be large groups of
  64.     any other type - even arrays!
  65.  
  66.     Here is a short list of the simple variable types in C, each detailing just
  67.     how much space one of these variables really takes up, and just what it can
  68.     represent.
  69.  
  70.         Byte
  71.             1 byte long
  72.             range: -128 to 127 (256 different values)
  73.         Unsigned Byte
  74.             1 byte long
  75.             Just like Byte but the range is 0 to 255 (256 altogether)
  76.         Integer
  77.             2 bytes long
  78.             range: -32768 to 32767 (65536 different values)
  79.         Unsigned Integer
  80.             2 bytes long
  81.             range: 0 to 65535 (65536 different values)
  82.         Longword
  83.             4 bytes long
  84.             range: -2147483650 to 214783649  (2^32 different values)
  85.         Unsigned Longword
  86.             4 bytes long
  87.             range: 0 to 4294967300 (2^32 different values)
  88.         Float
  89.             4 bytes long
  90.             range: -far to +far
  91.             precision is limited..  But the range is quite good
  92.         Double precision Float
  93.             8 bytes long
  94.             range: -farther to +farther
  95.             precision is Much better than that of Float.  But takes up twice as
  96.             much space
  97.         Character
  98.             1 byte long
  99.             contains ASCII characters, like A or c or &..
  100.     In C, there is no "string" type.  This is because, as far as the compiler is
  101.     concerned, a string is just an array of characters.  As in BASIC, arrays in
  102.     C can contain a list of anything - even arrays.  Another note:  In actual C,
  103.     there is no distinction between the "Byte" and the "Character" because they
  104.     both fit into exactly 1 byte, and one can perform the exact same arithmetic
  105.     operations on both.  One can even use character constants in expressions
  106.     involving bytes, or integers.
  107.  
  108. Q:  Hmm..  How are different variable types named?
  109. A:  In C, unlike in BASIC, you don't need to append a special character at the
  110.     end of the variable names, simply because the compiler already knows what
  111.     type of variable it is.  So, you don't have to say WOMBAT$ for a string, or
  112.     FOOBAR% for an integer.  In C, you only have to define a variable once, and
  113.     from there, the C compiler will know what you mean.  The process of actually
  114.     telling the compiler about a variable will be detailed in a later tutorial.
  115.  
  116. Q:  Well, that sounds good.  What does an average C program look like?  I mean,
  117.     the program lines.  Similar to BASIC?
  118. A:  Everything is organized into "functions".  A function is a routine that may
  119.     or may not return a value.  Parameters can be passed to it, if you want.
  120.     Look at this example program.  I will explain it as we go along.  In most
  121.     programs, the programmer will put in comments to the reader of the program
  122.     so they understand more of just how the program works.  These comments are
  123.     enclosed by two symbols - /* to begin a comment, and */ to end one.  Nothing
  124.     between these symbols will be looked at by the compiler.
  125.  
  126.     main()  /* The main function    */
  127.         {
  128.         printf("Hello World\n");    /* Print a message to the user.. */
  129.         }
  130.  
  131.     Let me explain.  "main" is the name of the function which is called when a
  132.     program is run, that is, the operating system and startup instructions call
  133.     main() automatically.  The body of the function is enclosed in "{" and "}".
  134.     The symbols could be translated as "Block Begin" and "Block End".  As you
  135.     will see, there are many places where these symbols are used, but they are
  136.     always used in pairs.  Functions usually have many statements between the
  137.     "{" and the "}" signs, but in this simple example, there is only one.
  138.     The third line contains one statement - a call to a function named printf().
  139.     It is passed as an argument the string "Hello World\n", where "\n" is a
  140.     special code "newline" which, on the Amiga, is translated into an LF
  141.     character (ASCII code #10.).  The argument to the printf() function is put
  142.     between a pair of parentheses - "()".  This function does not return any
  143.     values.  The statement, which is the call to printf(), is finished with a
  144.     ";" character.  All C statements must end with ";".  This is because the
  145.     compiler does not otherwise know when a statment ends, because it does not
  146.     care about the end of line.  To show this, look at this example, which is
  147.     quite identical, as far as the C compiler is concerned, to the first
  148.     example, above.
  149.  
  150.     main() { printf("Hello World\n"); }
  151.  
  152.     The matter of putting statements on separate lines is convention, which
  153.     makes reading programs much easier.
  154.  
  155. Q:  But, you said there was no variable type equivalent to strings in BASIC.
  156. A:  Good point.  Actually, since strings are very often used, C does support
  157.     a form of it, namely, enclosing characters in quotes (").  What it really
  158.     is translated to is an array of characters with a 00 byte at the end, to
  159.     show the end of the string.  The pr